<style lang='less' scoped>
	#btnsBox{
		text-align: left;
		button{
			background-color: transparent;
			color: #63c5ce;
			font-size: 14px;
			padding:0 10px;
			border-right: 1px solid #63c5ce;
			border-radius:0; 
			&:last-child{
				border:none;
			}
		}
		.fontColorGary{
			color: #bbb;
		}

	}
</style>

<template>
	<div id='btnsBox'>
		<button 
		v-if='btnControl && btnControl.check' 
		@click='goPage("couponListDetail",scope.row.uuid)'
		>详情</button>

		<button 
		v-if='btnControl && btnControl.start && status=="DISABLE"'  
		:class='{fontColorGary:(totalNum - takeNum) < 1}' 
		@click='handleCouponStatus(scope.$index, scope.row, (totalNum - takeNum) > 0)'
		>启动</button>

		<button 
		v-if='btnControl && btnControl.pause && status=="ENABLE"' 
		style='color:red' 
		@click='handleCouponStatus(scope.$index, scope.row)'
		>停用</button>

		<button 
		v-if='btnControl && btnControl.publish && status=="ENABLE" && canTakeTime' 
		:class='{fontColorGary:(totalNum - takeNum) < 1}'
		@click='goPage("couponListPublish",scope.row.uuid)'
		>发放</button>
	</div>
  
</template>

<script>
  import {postHandleCouponStatus} from 'api/coupon/index.js'
  import moment from 'moment';
  export default {
    props:['btnControl','status', 'totalNum' , 'takeNum','tekeStartDate','tekeEndDate','scope'],
    components:{
    },
    data(){
      return{
      }
    },
    computed:{
      canTakeTime(){//当前时间不在领取时间段时，发放按钮禁用
        let nowTime = moment().format('YYYY-MM-DD'),
            startTime = this.tekeStartDate,
            endTime = this.tekeEndDate;
        // console.log(`${this.scope.row.index} 现在时间:${nowTime}, 领取开始时间：${startTime}, 领取结束时间：${endTime} `, moment(startTime).isBefore(nowTime) || moment(startTime).isSame(nowTime))  && (moment(nowTime).isBefore(endTime) || moment(nowTime).isSame(endTime))
        return (moment(startTime).isBefore(nowTime) || moment(startTime).isSame(nowTime))  && (moment(nowTime).isBefore(endTime) || moment(nowTime).isSame(endTime))
      }
    },
    watch:{
    },
    methods:{
      async handleCouponStatus(index, row, num= true) {
        // console.log(index, row, num)
        if(num){
          let result = await this.postHandleCouponStatus(row.uuid)
          row.status = result ? result : row.status;
        }else{
          this.$alert('可发放数量小于0')
        }
      },
      async postHandleCouponStatus(uuid){
        try{
          let param ={
            uuid:uuid
          }
          let back = await postHandleCouponStatus(param);
          back = back.data;
          // console.log(`修改uuid => ${uuid} , ${back}`)
          if(back && back.resultCode =='200' && back.result){
              return back.result.status ;
          }else{
            this.$alert(back.msg)
          }
        }catch(err){
          console.error(err)
        }
      },
      goPage(name, uuid){
        if(name == 'couponListPublish' && this.totalNum - this.takeNum < 1){
          alert('可发放数量小于0')
          return
        }
        this.$emit('showPopup', true)
        this.$router.push({
          name:name,
          query:{
            uuid:uuid,
          }
      })
      }
    },
    created(){

    },
    mounted(){

    },
  }
</script>